<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\services\ExhibitionCategoryServices;

$categoryService = new ExhibitionCategoryServices();
$category = $categoryService->getData(null);

/* @var $this yii\web\View */
/* @var $model common\models\Exhibition */
/* @var $form yii\widgets\ActiveForm */
?>
<?=Html::jsFile('/static/common/js/ajaxfileupload.js')?>
<?=Html::jsFile('/static/common/js/underscore-min.js')?>
<?=Html::cssFile('/static/common/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css')?>
<?=Html::jsFile('/static/common/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js')?>
<style>
.exhibition-form {
    border: 1px solid #ddd;
}
.exhibition-form .nav-tabs-custom>.nav-tabs {padding-left: 5px;background-color: #337ab7;}
.nav-tabs-custom>.nav-tabs>li>a,.nav-tabs-custom>.nav-tabs>li>a:hover {color: #fff;}
.item_row {margin-bottom: 15px;}
.item_row input[type='text'],.item_row input[type='number'] {width: 160px!important;}
.item_row .input-group .input-group-addon {background-color: transparent; border: 0 none; cursor: pointer;}
.item_row .input-group .input-group-addon.bg-success {background-color: #5cb85c;}
.item_row .input-group .input-group-addon.bg-primary {background-color: #337ab7;}
.item_row .input-group .input-group-addon.bg-warning {background-color: #f0ad4e;}
.item_row .input-group .input-group-addon.bg-danger {background-color: #d9534f;}
.item_row .fa {color: #fff;}
.picture-list {}
.picture-list .picture-item {
    position: relative;
    height: 200px;
    margin-bottom: 15px;
}
.picture-list .picture-item img {
    max-height: 192px;
}
.picture-list .add .picture_btn {
    display: none;
}
.picture-list .picture-item .picture_btn {
    display: none;
    position: absolute;
    bottom: 21px;
    left: 0;
    width: 100%;
    height: 40px;
    padding: 3px 0;
    background-color: rgba(0,0,0,0.3);
    overflow: hidden;
}
.picture-list .picture-item:hover .picture_btn {
    display: block;
}
</style>
<div class="exhibition-form">
    <?php $form = ActiveForm::begin(); ?>
    <div class="nav-tabs-custom">
        <ul class="nav nav-tabs">
            <li class="active"><a href="#basic" data-toggle="tab" aria-expanded="true">基本信息</a></li>
            <li class=""><a href="#metting" data-toggle="tab" aria-expanded="false">同期会议</a></li>
            <li class=""><a href="#exhibitor" data-toggle="tab" aria-expanded="false">知名展商</a></li>
            <li class=""><a href="#zhuban" data-toggle="tab" aria-expanded="false">主办机构</a></li>
            <li class=""><a href="#chengban" data-toggle="tab" aria-expanded="false">承办机构</a></li>
            <li class=""><a href="#photo" data-toggle="tab" aria-expanded="false">展会集锦</a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane active" id="basic">
                <table class="table">
                    <tr>
                        <td width="50%">
                            <?= $form->field($model, 'title')->textInput(['maxlength' => true])->label(Yii::t('common', 'name')) ?>
                        </td>
                        <td>
                            <?= $form->field($model, 'status')->dropDownList(\common\helpers\TStringHelper::getStatusText()) ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="form-group">
                                <label class="control-label"><?=Yii::t('common', 'category')?></label>
                                <select id="exhibition-category_id" class="form-control" name="Exhibition[category_id]">
                                    <option value=""><?=Yii::t('common', 'select...')?></option>
                                    <?php
                                    if (!empty($category['parent'])) {
                                        foreach ($category['parent'] as $val) {
                                            ?>
                                            <option value="<?=$val['id']?>" <?=!empty($model->category_id)&&$model->category_id==$val['id'] ? 'selected="selected"':''?>><?=$val['name']?></option>
                                            <?php
                                            if (!empty($category['child'][$val['id']])){
                                                foreach ($category['child'][$val['id']] as $value) {
                                                    ?>
                                                    <option value="<?=$value['id']?>"<?=!empty($model->category_id)&&$model->category_id==$value['id'] ? 'selected="selected"':''?>>┗━<?=$value['name']?></option>
                                                    <?php
                                                }
                                            }
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                        </td>
                        <td>
                            <?= $form->field($model, 'hall_id')->dropDownList(\common\services\ExhibitionHallServices::getHallDropdownList(), ['prompt' => Yii::t('common', 'select...')]) ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <?= $form->field($model, 'logo')->textInput(['id' => 'logo']) ?>
                            <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach3" data-id="attach3">
                        </td>
                        <td>
                            <?= $form->field($model, 'litpic')->textInput(['id' => 'litpic']) ?>
                            <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach4" data-id="attach4">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label class="control-label"><?=Yii::t('common', 'starttime')?></label>
                            <div class="form-group">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="starttime" name="Exhibition[starttime]" value="<?=!empty($model->starttime)?date('Y-m-d H:i', $model->starttime):''?>" readonly placeholder="<?=Yii::t('common', 'click_select')?>">
                                    <span class="input-group-addon"><i class="fa fa-remove icon-remove"></i></span>
                                </div>
                            </div>
                        </td>
                        <td>
                            <label class="control-label"><?=Yii::t('common', 'endtime')?></label>
                            <div class="form-group">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="endtime" name="Exhibition[endtime]" value="<?=!empty($model->endtime)?date('Y-m-d H:i', $model->endtime):''?>" readonly placeholder="<?=Yii::t('common', 'click_select')?>">
                                    <span class="input-group-addon"><i class="fa fa-remove icon-remove"></i></span>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td><?= $form->field($model, 'area')->textInput(['type' => 'number']) ?></td>
                        <td><?= $form->field($model, 'website')->textInput() ?></td>
                    </tr>
                    <tr>
                        <td><?= $form->field($model, 'business_number')->textInput(['type' => 'number'])->hint('<a href="javascript:;" onclick="modalLoad(\'formModal\', \''.Yii::$app->urlManager->createUrl(['exhibition/get-business']).'\', \'展商构成\')">设置展商构成</a>') ?></td>
                        <td><?= $form->field($model, 'audience_number')->textInput(['type' => 'number'])->hint('<a href="javascript:;" onclick="modalLoad(\'formModal\', \''.Yii::$app->urlManager->createUrl(['exhibition/get-audience']).'\', \'观众构成\')">设置观众构成</a>') ?></td>
                    </tr>
                    <tr>
                        <td><?= $form->field($model, 'build_number')->textInput(['type' => 'number']) ?></td>
                        <td><?= $form->field($model, 'comments_number')->textInput(['type' => 'number']) ?></td>
                    </tr>
                    <tr>
                        <td>
                            <?= $form->field($model, 'bg')->textInput(['id' => 'bg']) ?>
                            <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach5" data-id="attach">
                        </td>
                        <td><?= $form->field($model, 'follow_number')->textInput(['type' => 'number'])->hint('用户关注') ?></td>
                    </tr>
                    <tr>
                        <td><?= $form->field($model, 'hot')->dropDownList(\common\helpers\TStringHelper::getExhibitionHotRank()) ?></td>
                        <td><?= $form->field($model, 'recommend')->dropDownList(\common\helpers\TStringHelper::getYesOrNo()) ?></td>
                    </tr>
                    <tr>
                        <td>
                            <?= $form->field($model, 'wechat_qrcode')->textInput(['id' => 'wechat_qrcode']) ?>
                            <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach" data-id="attach">
                        </td>
                        <td>
                            <?= $form->field($model, 'video')->textInput(['id' => 'video']) ?>
                            <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach2" data-id="attach2">
                        </td>
                    </tr>
                    <tr>
                        <td><?= $form->field($model, 'address')->textInput(['maxlength' => true]) ?></td>
                        <td><?= $form->field($model, 'score')->textInput(['type' => 'number']) ?></td>
                    </tr>
                    <tr>
                        <td><?= $form->field($model, 'tags')->textInput(['maxlength' => true])->hint('标签之间以英文逗号","分隔') ?></td>
                        <td><?= $form->field($model, 'views_num')->textInput(['type' => 'number'])->hint('前台用户浏览次数') ?></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <?= $form->field($model, 'introduce')->widget(\dosamigos\ckeditor\CKEditor::className(),[
                                'options' => ['row' => 10],
                                'preset' => 'basic',
                                'clientOptions' => [
                                    'filebrowserUploadUrl' => \yii\helpers\Url::toRoute(['common/url', 'event' => 0]),
                                ]
                            ]) ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <?= $form->field($model, 'scope')->widget(\dosamigos\ckeditor\CKEditor::className(),[
                                'options' => ['row' => 10],
                                'preset' => 'basic',
                                'clientOptions' => [
                                    'filebrowserUploadUrl' => \yii\helpers\Url::toRoute(['common/url', 'event' => 0]),
                                ]
                            ]) ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <?= $form->field($model, 'booth_info')->widget(\dosamigos\ckeditor\CKEditor::className(),[
                                'options' => ['row' => 10],
                                'preset' => 'basic',
                                'clientOptions' => [
                                    'filebrowserUploadUrl' => \yii\helpers\Url::toRoute(['common/url', 'event' => 0]),
                                ]
                            ]) ?>
                        </td>
                    </tr>
                </table>
            </div>
            <!-- /.tab-pane -->
            <div class="tab-pane" id="metting">
                <table class="table">
                    <tr>
                        <td width="50%">
                            <div class="form-group">
                                <label class="control-label" for="exhibition-title"><?=Yii::t('common', 'title')?></label>
                                <?=Html::textInput('ExhibitionMeeting[title]', $meeting->title, ['class' => 'form-control'])?>
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>
                            <div class="form-group">
                                <label class="control-label" for="exhibition-title"><?=Yii::t('common', 'starttime')?></label>
                                <div class="input-group">
                                    <input type="text" class="form-control form-date" name="ExhibitionMeeting[starttime]" value="<?=!empty($meeting->starttime)?date('Y-m-d'):''?>" readonly placeholder="<?=Yii::t('common', 'click_select')?>">
                                    <span class="input-group-addon"><i class="fa fa-remove icon-remove"></i></span>
                                </div>
                            </div>

                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>
                            <div class="form-group">
                                <label class="control-label" for="exhibition-title"><?=Yii::t('common', 'endtime')?></label>
                                <div class="input-group">
                                    <input type="text" class="form-control form-date" name="ExhibitionMeeting[endtime]" value="<?=!empty($meeting->endtime)?date('Y-m-d'):''?>" readonly placeholder="<?=Yii::t('common', 'click_select')?>">
                                    <span class="input-group-addon"><i class="fa fa-remove icon-remove"></i></span>
                                </div>
                            </div>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>
                            <div class="form-group">
                                <label class="control-label" for="exhibition-title"><?=Yii::t('common', 'address')?></label>
                                <?=Html::textInput('ExhibitionMeeting[address]', $meeting->address, ['class' => 'form-control'])?>
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>
                            <div class="form-group">
                                <label class="control-label" for="exhibition-title"><?=Yii::t('common', 'sponsor')?></label>
                                <?=Html::textInput('ExhibitionMeeting[sponsor]', $meeting->sponsor, ['class' => 'form-control'])?>
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td></td>
                    </tr>
                </table>
            </div>
            <!-- /.tab-pane -->
            <div class="tab-pane" id="exhibitor">
                <div class="row">
                    <div class="col-sm-5">
                        <input class="form-control search" data-target="avaliable" placeholder="<?=Yii::t('common', 'search')?>">
                        <select size="20" class="form-control list" data-target="avaliable"></select>
                    </div>
                    <div class="col-sm-1">
                        <br><br>
                        <a class="btn btn-success btn-assign" href="javascript:;" title="<?=Yii::t('common', 'assign')?>" data-target="avaliable">&gt;&gt; <i class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></i></a><br><br>
                        <a class="btn btn-danger btn-assign" href="javascript:;" title="<?=Yii::t('common', 'remove')?>" data-target="assigned">&lt;&lt; <i class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></i></a>
                    </div>
                    <div class="col-sm-5">
                        <input class="form-control search" data-target="assigned" placeholder="<?=Yii::t('common', 'search')?>">
                        <select name="exhibitor_id[]" multiple size="20" class="form-control list" data-target="assigned"></select>
                    </div>
                </div>
            </div>
            <!-- /.tab-pane -->
            <div class="tab-pane" id="zhuban">
                <?= $form->field($model, 'sponsor')->widget(\dosamigos\ckeditor\CKEditor::className(),[
                    'options' => ['row' => 10],
                    'preset' => 'basic',
                    'clientOptions' => [
                        'filebrowserUploadUrl' => \yii\helpers\Url::toRoute(['common/url', 'event' => 0]),
                    ]
                ])->label(false) ?>
            </div>
            <!-- /.tab-pane -->
            <div class="tab-pane" id="chengban">
                <?= $form->field($model, 'undertook')->widget(\dosamigos\ckeditor\CKEditor::className(),[
                    'options' => ['row' => 10],
                    'preset' => 'basic',
                    'clientOptions' => [
                        'filebrowserUploadUrl' => \yii\helpers\Url::toRoute(['common/url', 'event' => 0]),
                    ]
                ])->label(false) ?>
            </div>
            <!-- /.tab-pane -->
            <div class="tab-pane" id="photo">
                <p>注：图片大小820*425</p>
                <div class="picture-list clearfix">
                    <?php
                    $i = 1;
                    if (!empty($picture)) {
                        foreach ($picture as $val) {
                    ?>
                    <div class="col-xs-6 col-md-3 picture-item">
                        <a href="javascript:;" class="thumbnail"><img src="<?=$val['fileUrl']?>" onerror="this.src='/static/common/images/nopicture.png'" /></a>
                        <div class="picture_btn text-center">
                            <div class="hidden">
                                <textarea name="picture[]" class="picture_value" id="picture_<?=$i?>_input"><?=json_encode($val)?></textarea>
                                <input type="file" name="Filedata" class="picture_upload" id="picture_<?=$i?>" data-id="attach">
                            </div>
                            <div class="btn-group">
                                <button class="btn btn-large btn-primary picture_plus" type="button">上传</button>
                                <button class="btn btn-large btn-danger picture_remove" type="button">删除</button>
                            </div>
                        </div>
                    </div>
                    <?php
                        $i++;
                        }
                    }
                    ?>
                    <div class="col-xs-6 col-md-3 add" title="添加">
                        <a href="javascript:;" class="thumbnail"><img src="" onerror="this.src='/static/common/images/addpicture.png'" /></a>
                    </div>
                </div>
            </div>
            <!-- /.tab-pane -->
        </div>
        <!-- /.tab-content -->
    </div>

    <div class="hidden">
        <textarea name="business_input" id="business_input"><?=json_encode($business)?></textarea>
        <textarea name="audience_input" id="audience_input"><?=json_encode($audience)?></textarea>
    </div>

    <div class="form-group" style="padding-left: 30px;">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'create') : Yii::t('common', 'update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary btn-lg']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<script type="text/template" id="item_tpl">
    <%
    if (typeof obj.data == 'undefined' || obj.data.length == 0) {
    %>
    <div class="item_row show" data-sort="<%=obj.sort%>">
        <div class="form-group">
            <label>名称</label>
            <div class="input-group">
                <input type="text" class="form-control title" name="title[]" />
            </div>
            <label>数量 </label>
            <div class="input-group">
                <input type="text" class="form-control num" name="num[]" onkeyup="this.value=this.value.replace(/\D/g,'')" onpause="this.value=this.value.replace(/\D/g,'')" />
                <div class="input-group-addon bg-success <%=obj.down?'':'hidden'%> oper-down" data-oper="down"><i class="fa fa-arrow-down"></i></div>
                <div class="input-group-addon bg-primary <%=obj.up?'':'hidden'%> oper-up" data-oper="up"><i class="fa fa-arrow-up"></i></div>
                <div class="input-group-addon bg-warning <%=obj.plus?'':'hidden'%> oper-plus" data-oper="plus"><i class="fa fa-plus-square"></i></div>
                <div class="input-group-addon bg-danger <%=obj.minus?'':'hidden'%> oper-minus" data-oper="minus"><i class="fa fa-minus-square"></i></div>
            </div>
        </div>
    </div>
    <%
    }else{
        var size = _.size(obj.data);
        _.each(obj.data, function(v, k) {
    %>
    <div class="item_row show" data-sort="<%=k%>">
        <div class="form-group">
            <label>名称</label>
            <div class="input-group">
                <input type="text" class="form-control title" name="title[]" value="<%=v.title%>" />
            </div>
            <label>数量 </label>
            <div class="input-group">
                <input type="text" class="form-control num" name="num[]" value="<%=v.num%>" onkeyup="this.value=this.value.replace(/\D/g,'')" onpause="this.value=this.value.replace(/\D/g,'')" />
                <div class="input-group-addon bg-success <%=k<size-1?'':'hidden'%> oper-down" data-oper="down"><i class="fa fa-arrow-down"></i></div>
                <div class="input-group-addon bg-primary <%=k>0?'':'hidden'%> oper-up" data-oper="up"><i class="fa fa-arrow-up"></i></div>
                <div class="input-group-addon bg-warning oper-plus" data-oper="plus"><i class="fa fa-plus-square"></i></div>
                <div class="input-group-addon bg-danger <%=k>0?'':'hidden'%> oper-minus" data-oper="minus"><i class="fa fa-minus-square"></i></div>
            </div>
        </div>
    </div>
    <%
        })
    }
    %>
</script>

<script type="text/template" id="picture_tpl">
<div class="col-xs-6 col-md-3 picture-item">
    <a href="javascript:;" class="thumbnail"><img src="" onerror="this.src='/static/common/images/nopicture.png'" /></a>
    <div class="picture_btn text-center">
        <div class="hidden">
            <textarea name="picture[]" class="picture_value" id="picture_<%=obj.id%>_input"></textarea>
            <input type="file" name="Filedata" class="picture_upload" id="picture_<%=obj.id%>" data-id="attach">
        </div>
        <div class="btn-group">
            <button class="btn btn-large btn-primary picture_plus" type="button">上传</button>
            <button class="btn btn-large btn-danger picture_remove" type="button">删除</button>
        </div>
    </div>
</div>
</script>

<script>
    $(function () {
        //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        $("#upload_child_attach").change(function (e) {
            var val = e.target.value;
            var point = val.lastIndexOf(".");
            var type = val.substr(point).toLowerCase();
            if (type != ".jpg" && type != ".jpeg" && type != ".png") {
                alert('系统目前只支持.jpg、.jpeg、.png文件格式');
                return false;
            }
            $.ajaxFileUpload({
                //处理图片的脚本路径
                url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'qrcode', 'thumb' => '200,200'])?>',
                type: 'post',       //提交的方式
                secureuri: false,   //是否启用安全提交
                fileElementId: 'upload_child_attach',     //file控件ID
                dataType: 'json',  //服务器返回的数据类型
                success: function (data, status) {  //提交成功后自动执行的处理函数
                    if (data.error) {
                        alert(data.msg);
                        return false;
                    } else {
                        $("input[id='wechat_qrcode']").val(data.fileUrl);
                    }
                },
                error: function (data, status, e) {   //提交失败自动执行的处理函数
                    alert(e);
                }
            })
        })
        //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        $("#upload_child_attach2").change(function (e) {
            var val = e.target.value;
            var point = val.lastIndexOf(".");
            var type = val.substr(point).toLowerCase();
            if (type != ".mp4") {
                alert('系统目前只支持.mp4文件格式');
                return false;
            }
            $.ajaxFileUpload({
                //处理图片的脚本路径
                url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'video'])?>',
                type: 'post',       //提交的方式
                secureuri: false,   //是否启用安全提交
                fileElementId: 'upload_child_attach2',     //file控件ID
                dataType: 'json',  //服务器返回的数据类型
                success: function (data, status) {  //提交成功后自动执行的处理函数
                    if (data.error) {
                        alert(data.msg);
                        return false;
                    } else {
                        $("input[id='video']").val(data.fileUrl);
                    }
                },
                error: function (data, status, e) {   //提交失败自动执行的处理函数
                    alert(e);
                }
            })
        })

        //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        $("#upload_child_attach3").change(function (e) {
            var val = e.target.value;
            var point = val.lastIndexOf(".");
            var type = val.substr(point).toLowerCase();
            if (type != ".jpg" && type != ".jpeg" && type != ".png") {
                alert('系统目前只支持.jpg、.jpeg、.png文件格式');
                return false;
            }
            $.ajaxFileUpload({
                //处理图片的脚本路径
                url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'thumb', 'thumb' => '180,180'])?>',
                type: 'post',       //提交的方式
                secureuri: false,   //是否启用安全提交
                fileElementId: 'upload_child_attach3',     //file控件ID
                dataType: 'json',  //服务器返回的数据类型
                success: function (data, status) {  //提交成功后自动执行的处理函数
                    if (data.error) {
                        alert(data.msg);
                        return false;
                    } else {
                        $("input[id='logo']").val(data.fileUrl);
                    }
                },
                error: function (data, status, e) {   //提交失败自动执行的处理函数
                    alert(e);
                }
            })
        })
        //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        var old_val = '';
        $("#upload_child_attach4").change(function (e) {
            var val = e.target.value;
            if (old_val == val) return false;
            old_val = val;
            var point = val.lastIndexOf(".");
            var type = val.substr(point).toLowerCase();
            if (type != ".jpg" && type != ".jpeg" && type != ".png") {
                alert('系统目前只支持.jpg、.jpeg、.png文件格式');
                return false;
            }
            $.ajaxFileUpload({
                //处理图片的脚本路径
                url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'thumb', 'thumb' => '350,210|305,190|250,150|184,150|86,75'])?>',
                type: 'post',       //提交的方式
                secureuri: false,   //是否启用安全提交
                fileElementId: 'upload_child_attach4',     //file控件ID
                dataType: 'json',  //服务器返回的数据类型
                success: function (data, status) {  //提交成功后自动执行的处理函数
                    if (data.error) {
                        alert(data.msg);
                        return false;
                    } else {
                        $("input[id='litpic']").val(data.fileUrl);
                    }
                },
                error: function (data, status, e) {   //提交失败自动执行的处理函数
                    alert(e);
                }
            })
        })
        //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        $("#upload_child_attach5").change(function (e) {
            var val = e.target.value;
            var point = val.lastIndexOf(".");
            var type = val.substr(point).toLowerCase();
            if (type != ".jpg" && type != ".jpeg" && type != ".png") {
                alert('系统目前只支持.jpg、.jpeg、.png文件格式');
                return false;
            }
            $.ajaxFileUpload({
                //处理图片的脚本路径
                url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'thumb', 'thumb' => '250,150'])?>',
                type: 'post',       //提交的方式
                secureuri: false,   //是否启用安全提交
                fileElementId: 'upload_child_attach5',     //file控件ID
                dataType: 'json',  //服务器返回的数据类型
                success: function (data, status) {  //提交成功后自动执行的处理函数
                    if (data.error) {
                        alert(data.msg);
                        return false;
                    } else {
                        $("input[id='bg']").val(data.fileUrl);
                    }
                },
                error: function (data, status, e) {   //提交失败自动执行的处理函数
                    alert(e);
                }
            })
        })
        $("#starttime,#endtime,.form-date").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            //showMeridian: true,
            minView: "day",
            autoclose: true,
            todayBtn: true,
            language: 'zh'
        });
        $(".fa-remove").on('click', function(){
            $(this).parent().siblings("input").val('')
        })

        //
        var html = _.template($("#item_tpl").html());
        //添加一行
        $(document).on('click', '.oper-plus', function (e) {
//        var html = $(this).parents('.item_row');
            if ($(".create_data").find(".item_row.show").length >= 10) {
                return false;
            }
            var length = $(".create_data").find('.item_row').length;
            $(".create_data").append(html({sort: length, down: false, up: true, plus: true, minus: true}));
            $(".create_data").find(".item_row").eq(length-1).find('.oper-down').removeClass('hidden');
            var show = $(".create_data").find('.show').length;
            if (show == 2) {
                $(".create_data").find(".show").eq(0).find('.oper-down').removeClass('hidden');
            }
        })
        //删除一行
        $(document).on('click', '.oper-minus', function (e) {
            if ($(".create_data").find(".item_row.show").length < 2) {
                return false;
            }
            $(this).parents('.item_row').removeClass('show').addClass('hidden');
            var length = $(".create_data").find('.show').length;
            $(".create_data").find(".show").eq(length-1).find('.oper-down').addClass('hidden');
            if (length == 1) {
                $(".create_data").find(".item_row.hidden").remove();
                $(".create_data").find(".item_row").eq(0).data('sort', 0);
            }
        })
        //向下移动一行
        $(document).on('click', '.oper-down', function (e) {
            var parents = $(this).parents(".show");
            var index = $(".create_data > .show").index($(parents));
            var next = $(".create_data > .show").eq(index+1);
            var title = parents.find(".title").val();
            var num = parents.find(".num").val();
            var n_title = next.find(".title").val();
            var n_num = next.find(".num").val();
            parents.find(".title").val(n_title);
            parents.find(".num").val(n_num);
            next.find(".title").val(title);
            next.find(".num").val(num);
        })
        //向上移动一行
        $(document).on('click', '.oper-up', function (e) {
            var parents = $(this).parents(".show");
            var index = $(".create_data > .show").index($(parents));
            var prev = $(".create_data > .show").eq(index-1);
            var title = parents.find(".title").val();
            var num = parents.find(".num").val();
            var p_title = prev.find(".title").val();
            var p_num = prev.find(".num").val();
            parents.find(".title").val(p_title);
            parents.find(".num").val(p_num);
            prev.find(".title").val(title);
            prev.find(".num").val(num);
        })

        /*------------------------知名展商选择begin------------------------*/
        var _opts = {
            "items":{
                "avaliable":<?=json_encode($exhibitorAvaliable)?>,
                "assigned":<?=json_encode($exhibitorAssigned)?>
            }
        };
        $('i.glyphicon-refresh-animate').hide();

        function objKeySort(obj) {//排序的函数
            var newkey = Object.keys(obj).sort();
            //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
            var newObj = {};//创建一个新的对象，用于存放排好序的键值对
            for (var i = 0; i < newkey.length; i++) {//遍历newkey数组
                newObj[newkey[i]] = obj[newkey[i]];//向新创建的对象中按照排好的顺序依次增加键值对
            }
            return newObj;//返回排好序的新对象
        }

        function updateItems(r) {
            _opts.items.avaliable = objKeySort(r.avaliable);
            _opts.items.assigned = objKeySort(r.assigned);
            search('avaliable');
            search('assigned');
        }

        $('.btn-assign').click(function () {
            var $this = $(this);
            var target = $this.data('target');
            var items = $('select.list[data-target="' + target + '"]').val();
            var text = $('select.list[data-target="' + target + '"]').find("option:selected").text();

            if (items && items.length) {
                if (target == 'avaliable') {
                    delete _opts.items.avaliable[items];
                    _opts.items.assigned[items] = text;
                }else{
                    delete _opts.items.assigned[items];
                    _opts.items.avaliable[items] = text;
                }
                $this.children('i.glyphicon-refresh-animate').show();
                updateItems(_opts.items);
                $this.children('i.glyphicon-refresh-animate').hide();
                /*$this.children('i.glyphicon-refresh-animate').show();
                $.post($this.attr('href'), {items: items}, function (r) {
                    updateItems(r);
                }).always(function () {
                    $this.children('i.glyphicon-refresh-animate').hide();
                });*/
            }
            return false;
        });

        $('.search[data-target]').keyup(function () {
            search($(this).data('target'));
        });

        function search(target) {
            var $list = $('select.list[data-target="' + target + '"]');
            $list.html('');
            var q = $('.search[data-target="' + target + '"]').val();

            var groups = {
                //role: [$('<optgroup label="Roles">'), false],
                //permission: [$('<optgroup label="Permission">'), false],
                route: [$('<optgroup label="展商列表">'), false],
            };
            $.each(_opts.items[target], function (i, name) {
                if (name.indexOf(q) >= 0) {
                    if (target == 'assigned') {
                        $('<option>').attr('selected', 'selected').text(name).val(i).appendTo(groups['route'][0]);
                    }else {
                        $('<option>').text(name).val(i).appendTo(groups['route'][0]);
                    }
                    groups['route'][1] = true;
                }
            });
            $.each(groups, function () {
                if (this[1]) {
                    $list.append(this[0]);
                }
            });
        }

        // initial
        search('avaliable');
        search('assigned');
        /*------------------------知名展商选择END------------------------*/

        /**-----------------------图片---------------------------**/
        var picture_count = <?=$i?>;
        $(".add").on('click', function (e) {
            var html = _.template($("#picture_tpl").html())
            $(this).before(html({id: picture_count}))
            picture_count ++;
        })

        $(document).on('click', ".picture_plus", function () {
            $(this).parents('.picture-item').find('.picture_upload').trigger('click');
        })
        $(document).on('click', ".picture_remove", function () {
            var parent = $(this).parents(".picture-item");
            //上传过图片的才提示
            if(parent.find(".picture_value").val() != '') {
                if (!confirm('确认删除？')) {
                    return false;
                }
            }
            $(this).parents('.picture-item').remove();
        })
        $(document).on("change", ".picture_upload", function (e) {
            var val = e.target.value;
            var point = val.lastIndexOf(".");
            var type = val.substr(point).toLowerCase();
            if (type != ".jpg" && type != ".jpeg" && type != ".png") {
                alert('系统目前只支持.jpg、.jpeg、.png文件格式');
                return false;
            }
            var images = $(this).parents('.picture-item').find('img');
            var file_id = $(this).attr('id');
            var file_input = $("#"+file_id+'_input');
            $.ajaxFileUpload({
                //处理图片的脚本路径
                url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'exhibition_picture', 'thumb' => '175,110|820,425'])?>',
                type: 'post',       //提交的方式
                secureuri: false,   //是否启用安全提交
                fileElementId: file_id,     //file控件ID
                dataType: 'json',  //服务器返回的数据类型
                success: function (data, status) {  //提交成功后自动执行的处理函数
                    if (data.error) {
                        alert(data.msg);
                        return false;
                    } else {
                        file_input.text(JSON.stringify(data));
                        images.attr('src', data.fileUrl);
                    }
                },
                error: function (data, status, e) {   //提交失败自动执行的处理函数
                    alert(e);
                }
            })
        })

        /**-----------------------图片---------------------------**/
    })
</script>